<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>父组件向子组件传值</title>
    <script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
    <style>
      .parent {
        width: 600px;
        min-height: 300px;
        background-color: pink;
        padding: 20px;
      }
      .child {
        width: 80%;
        min-height: 100px;
        height:auto;
        padding: 0px 10px;
        border: 1px solid black;
        background-color: lightgreen;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <parent-component></parent-component>
    </div>
    <script>
      //1.声明父组件
      Vue.component("parent-component", {
        data() {
          return { datas: "来自父组件", title: "父组件的标题", lists: ['haoren','lucy','lily'] };
        },
        template: `<div class='parent'>
                     <h3>父组件</h3>
                     <child-component parentData='父组件的静态数据'></child-component>
                     <hr>
                     <child-component :parent-data='datas'></child-component>
                     <hr>
                     <child-component :title='title'></child-component>
                     <hr>
                     <child-component :array='lists'></child-component>
                   </div>`,
      });

      //2.声明子组件
      Vue.component("child-component", {
        props: ["parentData", "title","array"], //props必须是一个数组
        data() {
          return { selfData: "子组件的数据" };
        },
        template: `<div class='child'>
                     <h4>{{selfData}}</h4>
                     <h4>{{parentData}}</h4>
                     <h4>{{title}}</h4>
                     <hr>
                     <ul>
                       <li :key='index' v-for='(v,index) in array'>{{v}}</li>
                     </ul>
                 </div>`,
      });

      //1. 实例化一个Vue的实例
      let vm = new Vue({
        el: "#app",
        data: {},
      });
    </script>
  </body>
</html>
